#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.64])
AC_INIT([BabelSnake], [0.1], [chris.brumgard@gmail.com])
AC_CONFIG_SRCDIR([plugin])
AC_CONFIG_HEADERS([plugin/config.h])

AC_PROG_LIBTOOL
AC_DISABLE_SHARED

AC_CONFIG_MACRO_DIR([m4])

AM_INIT_AUTOMAKE()

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_CPP
AC_PROG_AWK


AC_ARG_WITH([protobuf], 
            [  --with-protobuf=protobuf-dir top level directory for protobuf installation],
            [protobuf="$with_protobuf"],
            [protobuf="no"])

AC_ARG_WITH([swig],
            [  --with-swig=path-to-swig  full path to the swig executable],
            [swigbin="$with_swig"],
            [swigbin="swig"])

AC_ARG_WITH([python],
            [  --with-python=path-to-python],
            [python="$with_python"],
            [python="python"])


if test "x$protobuf" != "xno"; then
    PROTOBUF_INCLUDE="-I$protobuf/include"
    PROTOBUF_LDFLAGS="-L$protobuf/lib"
else
    PROTOBUF_INCLUDE=""
    PROTOBUF_LDFLAGS=""
fi



# Gets the include directory for swig
SWIG_INC=`$swigbin -swiglib`
SWIG_INC="\"-I$SWIG_INC\""


# Gets the settings for Python
PYTHON=$python

# Gets the major version of python
PYVERSION=`$python -c 'import sys; sys.stdout.write(str(sys.version_info[[0]]))'`

# Makefile python conditional
AM_CONDITIONAL(PY3K, [test "$PYVERSION" == "3"])


# Defines the major version of python
AC_DEFINE_UNQUOTED([PYVERSION], $PYVERSION, [Major version of python])

# Tests if the provided swig can handle python 3.0
if test "$PYVERSION" == "3"; then

    result=`$swigbin -version | $AWK '{ if($NF > 0) { split($3, a, "."); if(a[[1]] < 1 || a[[2]] < 2 || a[[3]] < 37) {print("bad");} exit;} }'`

    if test "x$result" != "x"; then
        echo "Swig version failed for Python 3.0 support, please upgrade to a newer version of swig"
        exit
    fi
fi

# Defines the value of SWIG_BIN and the SWIG_INCq
SWIG_BIN="\"$swigbin\""
AC_DEFINE_UNQUOTED([SWIG_BIN], $SWIG_BIN, [Path to swig binary])
AC_DEFINE_UNQUOTED([SWIG_INC], $SWIG_INC, [Path to swig include directory])


# Defines the value for PROTOBUF_INC
AC_DEFINE_UNQUOTED([PROTOBUF_INC], "$PROTOBUF_INCLUDE", [Path to the include directory for protobuf])



# Checks for libraries.
# FIXME: Replace `main' with a function in `-lprotobuf':
#AC_CHECK_LIB([protobuf], [main])
# FIXME: Replace `main' with a function in `-lprotoc':
#AC_CHECK_LIB([protoc], [main])

# Checks for header files.
AC_CHECK_HEADERS([float.h limits.h string.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_INLINE
AC_TYPE_PID_T
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_FORK
AC_FUNC_MALLOC
AC_CHECK_FUNCS([dup2 floor memmove memset strstr])


AC_SUBST(PROTOBUF_INCLUDE)
AC_SUBST(PROTOBUF_LDFLAGS)
AC_SUBST(PYTHON)
AC_SUBST(SWIG_BIN)

AC_CONFIG_FILES([Makefile
                 plugin/Makefile
                 protobuf/Makefile
                 protobuf/setup.py])
AC_OUTPUT
